/** 模拟岗位发布接口响应数据 */
import type { PositionApplication, PositionInfo, PositionQuery } from "./type"

// 模拟岗位数据列表
const MOCK_POSITION_LIST: PositionInfo[] = [
  {
    id: 1,
    enterpriseId: 1,
    enterpriseName: "北京科技有限公司",
    positionName: "前端开发实习生",
    positionType: "development",
    workType: "offline",
    workCity: "北京市",
    salaryRange: "150-200/day",
    internshipPeriod: "3m",
    startDate: "2025-11-16",
    recruitmentCount: 3,
    majorRequirements: ["cs", "se"],
    skillRequirements: "熟悉HTML、CSS、JavaScript，了解Vue框架",
    jobResponsibilities: "协助前端开发工作，参与页面实现和功能开发",
    jobDescriptionUrl: "https://example.com/description1.pdf",
    applicationDeadline: "2024-01-20",
    contactInfo: "hr@example.com",
    status: "recruiting",
    createTime: "2024-01-01 10:00:00"
  },
  {
    id: 2,
    enterpriseId: 2,
    enterpriseName: "上海创新科技有限公司",
    positionName: "人工智能算法实习生",
    positionType: "research",
    workType: "online",
    workCity: "",
    salaryRange: "200-300/day",
    internshipPeriod: "6m",
    startDate: "2025-12-16",
    recruitmentCount: 2,
    majorRequirements: ["cs", "data-science"],
    skillRequirements: "熟悉Python，了解机器学习算法",
    jobResponsibilities: "参与AI算法研究和优化，协助数据处理工作",
    jobDescriptionUrl: "https://example.com/description2.pdf",
    applicationDeadline: "2024-01-30",
    contactInfo: "recruit@example.com",
    status: "recruiting",
    createTime: "2024-01-05 14:30:00"
  },
  {
    id: 3,
    enterpriseId: 1,
    enterpriseName: "北京科技有限公司",
    positionName: "产品经理实习生",
    positionType: "product",
    workType: "offline",
    workCity: "北京市",
    salaryRange: "120-180/day",
    internshipPeriod: "3m",
    startDate: "2025-11-28",
    recruitmentCount: 1,
    majorRequirements: ["cs", "se", "any"],
    skillRequirements: "了解产品设计流程，具备良好的沟通能力",
    jobResponsibilities: "协助产品经理进行需求分析和文档编写",
    jobDescriptionUrl: "https://example.com/description3.pdf",
    applicationDeadline: "2024-01-15",
    contactInfo: "product@example.com",
    status: "closed",
    createTime: "2024-01-10 09:15:00"
  }
]

/** 模拟获取岗位列表接口 */
export function getPositionListApiMock(params: PositionQuery) {
  return new Promise<{ code: number, data: { list: PositionInfo[], total: number }, message: string }>((resolve) => {
    setTimeout(() => {
      // 模拟分页和搜索逻辑
      let filteredList = [...MOCK_POSITION_LIST]

      // 搜索过滤
      if (params.positionName) {
        filteredList = filteredList.filter(item =>
          item.positionName.includes(params.positionName!)
        )
      }

      if (params.enterpriseName) {
        filteredList = filteredList.filter(item =>
          item.enterpriseName?.includes(params.enterpriseName!)
        )
      }

      if (params.status) {
        filteredList = filteredList.filter(item => item.status === params.status)
      }

      // 分页
      const total = filteredList.length
      const start = (params.currentPage - 1) * params.size
      const end = start + params.size
      const paginatedList = filteredList.slice(start, end)

      resolve({
        code: 0,
        data: {
          list: paginatedList,
          total
        },
        message: "获取成功"
      })
    }, 500)
  })
}

/** 模拟创建岗位接口 */
export function createPositionApiMock(data: Omit<PositionInfo, "id" | "createTime" | "enterpriseName">) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      // 查找企业名称
      const enterpriseMap: Record<number, string> = {
        1: "北京科技有限公司",
        2: "上海创新科技有限公司",
        3: "广州数字技术有限公司"
      }

      const newPosition: PositionInfo = {
        ...data,
        id: Date.now(),
        enterpriseName: enterpriseMap[data.enterpriseId] || "未知企业",
        createTime: new Date().toLocaleString("zh-CN")
      }
      MOCK_POSITION_LIST.unshift(newPosition)
      resolve({ code: 0, message: "创建成功" })
    }, 500)
  })
}

/** 模拟更新岗位接口 */
export function updatePositionApiMock(data: PositionInfo) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      const index = MOCK_POSITION_LIST.findIndex(item => item.id === data.id)
      if (index !== -1) {
        // 查找企业名称
        const enterpriseMap: Record<number, string> = {
          1: "北京科技有限公司",
          2: "上海创新科技有限公司",
          3: "广州数字技术有限公司"
        }

        MOCK_POSITION_LIST[index] = {
          ...data,
          enterpriseName: enterpriseMap[data.enterpriseId] || "未知企业"
        }
        resolve({ code: 0, message: "更新成功" })
      } else {
        resolve({ code: 404, message: "岗位不存在" })
      }
    }, 500)
  })
}

/** 模拟更新岗位状态接口 */
export function updatePositionStatusApiMock(id: number, status: string) {
  return new Promise<{ code: number, message: string }>((resolve) => {
    setTimeout(() => {
      const position = MOCK_POSITION_LIST.find(item => item.id === id)
      if (position) {
        position.status = status
        resolve({ code: 0, message: "状态更新成功" })
      } else {
        resolve({ code: 404, message: "岗位不存在" })
      }
    }, 500)
  })
}

/** 模拟上传岗位说明书接口 */
export function uploadJobDescriptionApiMock(file: File) {
  return new Promise<{ code: number, data: { url: string }, message: string }>((resolve) => {
    setTimeout(() => {
      const url = `https://example.com/jobdesc/${Date.now()}_${file.name}`
      resolve({
        code: 0,
        data: { url },
        message: "上传成功"
      })
    }, 1000)
  })
}

/** 模拟学生岗位列表数据 */
export function getStudentPositionListMock(_query: any) {
  return Promise.resolve({
    code: 200,
    data: {
      list: MOCK_POSITION_LIST.map(pos => ({
        ...pos,
        applicationCount: Math.floor(Math.random() * 50),
        enterpriseContactInfo: "138****5678"
      })),
      total: MOCK_POSITION_LIST.length
    },
    message: "success"
  })
}

/** 模拟学生岗位详情数据 */
export function getStudentPositionDetailMock(id: number) {
  const position = MOCK_POSITION_LIST.find(pos => pos.id === id)
  if (!position) {
    return Promise.resolve({ code: 404, data: null, message: "岗位不存在" })
  }
  return Promise.resolve({
    code: 200,
    data: {
      ...position,
      applicationCount: Math.floor(Math.random() * 50),
      enterpriseContactInfo: "138****5678"
    },
    message: "success"
  })
}

/** 模拟岗位申请数据 */
const mockApplications: PositionApplication[] = [
  {
    id: 1,
    positionId: 1,
    positionName: "Java开发实习生",
    studentId: 1001,
    studentName: "张三",
    major: "计算机科学与技术",
    applyTime: "2023-10-15T08:30:00Z",
    expectedStartDate: "2023-11-01",
    expectedEndDate: "2024-01-31",
    contactPhone: "13812345678",
    emergencyContact: { name: "李四", phone: "13987654321" },
    interviewProofUrl: "/mock/proof/1.pdf",
    status: "pending"
  },
  {
    id: 2,
    positionId: 2,
    positionName: "前端开发实习生",
    studentId: 1002,
    studentName: "王五",
    major: "软件工程",
    applyTime: "2023-10-16T10:20:00Z",
    expectedStartDate: "2023-11-05",
    expectedEndDate: "2024-02-05",
    contactPhone: "13712345678",
    emergencyContact: { name: "赵六", phone: "13687654321" },
    interviewProofUrl: "/mock/proof/2.pdf",
    status: "approved"
  }
]

/** 创建岗位申请 */
export function createPositionApplicationMock(data: any) {
  const newApplication: PositionApplication = {
    ...data,
    id: mockApplications.length + 1,
    positionName: MOCK_POSITION_LIST.find(p => p.id === data.positionId)?.positionName || "",
    applyTime: new Date().toISOString(),
    status: "pending"
  }
  mockApplications.push(newApplication)
  return Promise.resolve({
    code: 200,
    data: newApplication,
    message: "申请提交成功"
  })
}

/** 获取岗位申请列表 */
export function getPositionApplicationsMock(query: any) {
  let filtered = [...mockApplications]
  if (query.status) {
    filtered = filtered.filter(app => app.status === query.status)
  }
  if (query.positionId) {
    filtered = filtered.filter(app => app.positionId === query.positionId)
  }
  return Promise.resolve({
    code: 200,
    data: {
      list: filtered,
      total: filtered.length
    },
    message: "success"
  })
}

/** 审核岗位申请 */
export function reviewPositionApplicationMock(id: number, data: { status: "approved" | "rejected", comment?: string }) {
  const application = mockApplications.find(app => app.id === id)
  if (!application) {
    return Promise.resolve({ code: 404, message: "申请不存在" })
  }
  application.status = data.status
  application.reviewComment = data.comment
  return Promise.resolve({
    code: 200,
    data: application,
    message: "审核成功"
  })
}

/** 收藏/取消收藏岗位 */
export function toggleFavoriteMock(positionId: number, isFavorite: boolean) {
  return Promise.resolve({
    code: 200,
    data: isFavorite ? { id: 1, positionId, studentId: 1001, createTime: new Date().toISOString() } : undefined,
    message: isFavorite ? "收藏成功" : "取消收藏成功"
  })
}
